package com.zzsy.shoppingpro.type.framgent;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/8/17 0017.
 */

public class TypeFragment extends BaseFragment{
    private SegmentTabLayout segmentTabLayout;
    private ImageView iv_type_search;
    private FrameLayout fl_type;
    private List<BaseFragment> fragmentList;
    private ListFragment listFragment;
    private TagFragment tagFragment;
    private Fragment tempFragment;
    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.fragment_type, null);
        initUI(view);
        return view;
    }

    /**
     * 初始化控件
     * */
    private void initUI(View view) {
        segmentTabLayout = view.findViewById(R.id.tl_1);
        fl_type = view.findViewById(R.id.fl_type);
        iv_type_search = view.findViewById(R.id.tv_type_search);
    }

    /**
     * 初始化数据
     * */
    @Override
    public void initData() {
    initFragment();
    String[] titles = {"分类","标签"};
    segmentTabLayout.setTabData(titles);
    segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            switchFragment(tempFragment,fragmentList.get(position));
        }

        @Override
        public void onTabReselect(int position) {

        }
    });
    }

    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if(tempFragment != nextFragment){
            tempFragment = nextFragment;
            if(nextFragment != null){
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if(!nextFragment.isAdded()){
                    // 隐藏当前framgent
                    if(fromFragment != null){
                       transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.fl_type,nextFragment,"tagFragment").commit();
                }else{
                    //隐藏当前framgnet
                    if(fromFragment != null){
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }

    /**
     * 初始化fragment数据
     * */
    private void initFragment() {
        fragmentList = new ArrayList<>();
        listFragment = new ListFragment();
        tagFragment = new TagFragment();
        fragmentList.add(listFragment);
        fragmentList.add(tagFragment);
        switchFragment(tempFragment,fragmentList.get(0));
    }

    @Override
    public void onResume() {
        super.onResume();
        switchFragment(tempFragment,fragmentList.get(0));
    }
}
